package com.quectel.aiot.service.impl;

import com.quectel.aiot.entity.Product;
import com.quectel.aiot.mapper.ProductMapper;
import com.quectel.aiot.service.ProductService;
import com.quec.client.MgrClient;
import com.quec.model.product.request.ProductListRequest;
import com.quec.model.product.response.ProductListResponse;
import com.quec.model.product.response.ProductListResponseBody;
import com.quec.model.project.request.ProjectListRequest;
import com.quec.model.project.response.ProjectListResponse;
import com.quec.model.project.response.ProjectListResponseBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private MgrClient mgrClient;

    @Override
    public void syncProducts(String projectId) {
        try {
            // 创建产品列表请求
            ProductListRequest request = new ProductListRequest(projectId);
            // 调用SDK获取产品列表
            ProductListResponse response = mgrClient.getProductList(request);
            
            if (response != null && response.getSuccess() && response.getBody() != null) {
                for (ProductListResponseBody productInfo : response.getBody()) {
                    // 检查产品是否已存在
                    Product existingProduct = productMapper.findByProductKey(productInfo.getProductKey());
                    
                    Product product = new Product();
                    product.setProductKey(productInfo.getProductKey());
                    product.setProductName(productInfo.getProductName());
                    product.setProductDesc(productInfo.getNetWay());
                    product.setProjectId(projectId);
                    product.setAuthType(productInfo.getAuthType());
                    product.setNetType(productInfo.getAccessType());
                    product.setNodeType(productInfo.getAuthStatus());
                    product.setStatus(productInfo.getProductStatus());
                    product.setUpdateTime(new Date());

                    if (existingProduct == null) {
                        // 新增产品
                        product.setCreateTime(new Date());
                        productMapper.insert(product);
                        log.info("新增产品成功: {}", product.getProductKey());
                    } else {
                        // 更新产品
                        productMapper.update(product);
                        log.info("更新产品成功: {}", product.getProductKey());
                    }
                }
            } else {
                log.error("获取产品列表失败: {}", response != null ? response.getErrorMsg() : "response is null");
            }
        } catch (Exception e) {
            log.error("同步产品列表失败", e);
            throw new RuntimeException("同步产品列表失败", e);
        }
    }

    @Override
    public void syncAllProjectsProducts() {
        try {
            // 1. 获取所有项目列表
            ProjectListRequest projectListRequest = new ProjectListRequest();
            ProjectListResponse projectListResponse = mgrClient.getProjectList(projectListRequest);
            
            if (projectListResponse != null && projectListResponse.getSuccess() && 
                projectListResponse.getBody() != null) {
                
                // 2. 遍历每个项目
                for (ProjectListResponseBody project : projectListResponse.getBody()) {
                    try {
                        log.info("开始同步项目[{}]的产品列表", project.getProjectId());
                        // 3. 同步当前项目的产品列表
                        syncProducts(project.getProjectId());
                        log.info("完成同步项目[{}]的产品列表", project.getProjectId());
                    } catch (Exception e) {
                        log.error("同步项目[{}]的产品列表失败", project.getProjectId(), e);
                        // 继续处理下一个项目
                        continue;
                    }
                }
            } else {
                log.error("获取项目列表失败: {}", 
                    projectListResponse != null ? projectListResponse.getErrorMsg() : "response is null");
            }
        } catch (Exception e) {
            log.error("同步所有项目产品列表失败", e);
            throw new RuntimeException("同步所有项目产品列表失败", e);
        }
    }

    @Override
    public List<Product> getAllProducts() {
        return productMapper.findAll();
    }
} 